package com.yuwei.user

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseMviActivity
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.common.mvi.UiState
import com.yuwei.user.databinding.ActivityUserLoginBinding
import com.yuwei.user.entity.GetPermissions
import com.yuwei.user.entity.UserEntity
import com.yuwei.user.intent.UserIntent
import com.yuwei.user.viewmodel.UserViewModel

@Route(path = RoutePath.MODULE_USER_LOGIN_ACTIVITY)
class UserLoginActivity : BaseMviActivity<ActivityUserLoginBinding,UserViewModel>() {
    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getUserLogin"->{
                val data = it.data as UserEntity
                MMKV.defaultMMKV().encode("token",data.token)
                MMKV.defaultMMKV().encode("phone",data.username)
                MMKV.defaultMMKV().encode("userId",data.id)
                if (GetPermissions.hasContactsPermission(this)) {
                    ARouter.getInstance().build(RoutePath.MAIN_ACTIVITY).navigation()
                }else{
                    ARouter.getInstance().build(RoutePath.MODULE_PHONE_BOOK_ACTIVITY).navigation()
                }
                LogUtils.i("登录成功! ${data.username}")
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.tvHelp.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_LOGIN_HELP_ACTIVITY).navigation()
        }
        val spannable = SpannableStringBuilder(binding.tvProtocol.text.toString())
        val spannable1 = SpannableStringBuilder(binding.tvPrivacy.text.toString())
        val spannable2 = SpannableStringBuilder(binding.tvPass.text.toString())
        spannable.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable1.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable2.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://terms.alicdn.com/legal-agreement/terms/suit_bu1_unification/suit_bu1_unification202005142208_14749.html")
                    .navigation()
            }
        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvProtocol.setText(spannable)
        binding.tvProtocol.setMovementMethod(LinkMovementMethod.getInstance())
        spannable1.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://www.douyin.com/draft/douyin_agreement/douyin_agreement_privacy.html?id=6773901168964798477")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPrivacy.setText(spannable1)
        binding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance())
        spannable2.setSpan(object :ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://accounts.qq.com/find/password?from=11")
                    .navigation()


            }
        },0,4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPass.setText(spannable2)
        binding.tvPass.setMovementMethod(LinkMovementMethod.getInstance())
        binding.btnLogin.setOnClickListener {
//            if (!RegexUtils.isMobileExact(binding.etPhoneLogin.text)){
//                binding.etPhoneLogin.error = "请输入正确的手机号"
//                ToastUtils.showShort("请输入正确的手机号")
//                return@setOnClickListener
//            }
//            if (binding.etPhoneLogin.text.isEmpty()){
//                binding.etPhoneLogin.error = "请输入手机号"
//                ToastUtils.showShort("请输入手机号")
//                return@setOnClickListener
//            }
            if(binding.etPasswordLogin.text.isEmpty()){
                binding.etPasswordLogin.error = "请输入密码"
                ToastUtils.showShort("请输入密码")
                return@setOnClickListener
            }
            vm.sendIntent(
                UserIntent.getUserLogin(
                    binding.etPasswordLogin.text.toString(),
                    binding.etPhoneLogin.text.toString()
                )
            )
        }
    }

    override fun initData() {

    }

}